package de.dubiel.plung.client.pages.game;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

import de.dubiel.plung.client.events.UserDataSavedEvent;
import de.dubiel.plung.client.events.UserDataSavedHandler;
import de.dubiel.plung.client.gin.ClientGinjector;
import de.dubiel.plung.client.play.GameManager;

public class GameViewImpl extends Composite implements GameView {
    private static ActionUiBinder uiBinder = GWT.create(ActionUiBinder.class);

    interface ActionUiBinder extends UiBinder<Widget, GameViewImpl> {
    }

    private ClientGinjector clientGinjector;

    @UiField(provided = true)
    GameManager gameManager;
    @UiField
    Button startButton;
    @UiField
    Button stopButton;

    @Inject
    public GameViewImpl(ClientGinjector clientGinjector) {
        this.clientGinjector = clientGinjector;
        gameManager = this.clientGinjector.getGameManager();
        initWidget(uiBinder.createAndBindUi(this));
        gameManager.init();
        this.clientGinjector.getEventBus().addHandler(UserDataSavedEvent.TYPE, new UserDataSavedHandler() {
            @Override
            public void onUserDataSaved(UserDataSavedEvent event) {
                gameManager.updateGameElementProperties();
                gameManager.update();
            }
        });
    }

    @UiHandler("startButton")
    protected void onStart(ClickEvent event) {
        gameManager.startGame();
    }

    @UiHandler("stopButton")
    protected void onStop(ClickEvent event) {
        gameManager.stopGame();
    }
}